#!/bin/bash

# initialization script erawrim
# this script must be run by the user

ERAWRIM_PATH=~/.erawrim
DEVICE=""

function prepare_home ()
{
  if [ ! -e $ERAWRIM_PATH ]; then mkdir $ERAWRIM_PATH; fi;
  
  #This file specifies the state of the mirror
  echo readerup > $ERAWRIM_PATH/state_mirror

  #This file associates an identifier tag to a name
  touch $ERAWRIM_PATH/directory
  
  #This folder contains the list of Ztamps posed at a time t
  if [ ! -e $ERAWRIM_PATH/ztamps ]; then mkdir $ERAWRIM_PATH/ztamps; fi;

  if [ ! -e $ERAWRIM_PATH/readerup.sh ]
  then
    echo "#!/bin/bash" > $ERAWRIM_PATH/readerup.sh
    echo "DEVICE=\$(cat $ERAWRIM_PATH/detected_device)" >> $ERAWRIM_PATH/readerup.sh
    echo "echo \"The \$DEVICE reader has returned to normal operation.\"" >> $ERAWRIM_PATH/readerup.sh
  fi
  if [ ! -e $ERAWRIM_PATH/readerdown.sh ]
  then
    echo "#!/bin/bash" > $ERAWRIM_PATH/readerdown.sh
    echo "DEVICE=\$(cat $ERAWRIM_PATH/detected_device)" >> $ERAWRIM_PATH/readerdown.sh
    echo "echo \"The \$DEVICE reader is up-side-down, flip over to use again.\"" >> $ERAWRIM_PATH/readerdown.sh
  fi
  if [ ! -e $ERAWRIM_PATH/conected_default.sh ]; then echo "#!/bin/bash" > $ERAWRIM_PATH/conected_default.sh; fi;
  if [ ! -e $ERAWRIM_PATH/removed_default.sh ]; then echo "#!/bin/bash" > $ERAWRIM_PATH/removed_default.sh; fi;
  
  chmod +x $ERAWRIM_PATH/readerup.sh $ERAWRIM_PATH/readerdown.sh
}

function detected_mirror ()
{
  if [ ! -e $ERAWRIM_PATH/detected_device ]
  then
    while [ -z $DEVICE ]
    do
      #before connecting
      ls -1 /dev/hidraw* > /tmp/mirror_detected1
      
      read -p "Connect the Mir:ror, then press enter: "
      
      #after connecting
      ls -1 /dev/hidraw* > /tmp/mirror_detected2
      
      DEVICE=$(diff /tmp/mirror_detected1 /tmp/mirror_detected2 | tail -n 1 | cut -d " " -f 2)
    done
    #This file specifies the address of the mirror
    echo $DEVICE > $ERAWRIM_PATH/detected_device
    
    #Cleaning
    rm /tmp/mirror_detected1 /tmp/mirror_detected2
  else
    DEVICE=$(cat $ERAWRIM_PATH/detected_device)
  fi
}

function associated_tag_name ()
{
  ZTAMP_NAME=$(cat $ERAWRIM_PATH/directory | grep ^$1 | cut -d ':' -f 2)
}

function conected ()
{
  associated_tag_name $1
  if [ -z $ZTAMP_NAME ]
  then
    until [ ! -z $ZTAMP_NAME ]; do
      read -p "Name the new Ztamp:" ZTAMP_NAME
    done
    if [ -e $ERAWRIM_PATH/$ZTAMP_NAME ]
    then
      echo "Ztamp already registered, remove and touch again."; exit 1;
    fi
    mkdir $ERAWRIM_PATH/$ZTAMP_NAME
    echo $1:$ZTAMP_NAME >> $ERAWRIM_PATH/directory
    
    cat $ERAWRIM_PATH/conected_default.sh > $ERAWRIM_PATH/$ZTAMP_NAME/conected.sh
    chmod +x $ERAWRIM_PATH/$ZTAMP_NAME/conected.sh
    cat $ERAWRIM_PATH/removed_default.sh > $ERAWRIM_PATH/$ZTAMP_NAME/removed.sh
    chmod +x $ERAWRIM_PATH/$ZTAMP_NAME/removed.sh
    touch $ERAWRIM_PATH/$ZTAMP_NAME/lasttime
    touch $ERAWRIM_PATH/$ZTAMP_NAME/times
    
    echo 0 > $ERAWRIM_PATH/$ZTAMP_NAME/times
    date > $ERAWRIM_PATH/$ZTAMP_NAME/lasttime
    
    touch $ERAWRIM_PATH/ztamps/$ZTAMP_NAME
  else
    $ERAWRIM_PATH/$ZTAMP_NAME/conected.sh
    
    TIMES=$(cat $ERAWRIM_PATH/$ZTAMP_NAME/times)
    echo $(($TIMES+1)) > $ERAWRIM_PATH/$ZTAMP_NAME/times
    date > $ERAWRIM_PATH/$ZTAMP_NAME/lasttime
    
    touch $ERAWRIM_PATH/ztamps/$ZTAMP_NAME
  fi
}

function removed ()
{
  associated_tag_name $1
  if [ -z $ZTAMP_NAME ]
  then
    exit 1;
  else
    $ERAWRIM_PATH/$ZTAMP_NAME/removed.sh
    rm -f $ERAWRIM_PATH/ztamps/$ZTAMP_NAME
  fi
}

function start ()
{
  prepare_home
  detected_mirror
  if [ ! -r $DEVICE ]; then sudo chmod a+r $DEVICE; fi;
  ./erawrimd $DEVICE
}

function stop ()
{
  local PID
  PID=$(pidof erawrimd)
  
  kill -TERM $PID
}

case $1 in
  "start" ) start;;
  "stop" ) stop;;
  "clean" ) rm -f $ERAWRIM_PATH/detected_device; detected_mirror;;
  "readerup" ) $ERAWRIM_PATH/readerup.sh;;
  "readerdown" ) $ERAWRIM_PATH/readerdown.sh;;
  "conected" ) conected $2;;
  "removed" ) removed $2;;
  * ) echo impossible;;
esac
